package com.grom.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.grom.constant.Constants;

public class XmlParser {
	private String className = "";
	private List<String> allProperties = new ArrayList<String>();
	private Map<String, String> typeContainer = new HashMap<String, String>();

	public XmlParser() {
		this(Constants.XML_NAME);
	}

	public XmlParser(String filePath) {
		try {
			initParse(new FileInputStream(filePath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public XmlParser(File filePath) {
		try {
			initParse(new FileInputStream(filePath));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public XmlParser(InputStream ins) {
		initParse(ins);

	}

	public void initParse(InputStream ins) {
		try {
			System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
					"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document dom = builder.parse(ins);
			Element root = dom.getDocumentElement();
			className = root.getAttribute(Constants.KEY_FOR_NAME);
			NodeList nodeList = root.getElementsByTagName(Constants.KEY_FOR_PROPERTY);
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node node = nodeList.item(i);
				if (node == null) {
					continue;
				}
				NamedNodeMap map = node.getAttributes();
				String property = map.getNamedItem(Constants.KEY_FOR_COLUMNTYPE).getNodeValue();
				String name = map.getNamedItem(Constants.KEY_FOR_NAME).getNodeValue();
				typeContainer.put(name, property);
				allProperties.add(name);
			}
			System.out.println(typeContainer);
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * return all the properties defined in the xml file
	 * 
	 * @return
	 */
	public List<String> getAllProperties() {
		return allProperties;
	}

	public Map<String, String> getAllPropertiesName() {
		Map<String, String> allName = new HashMap<String, String>();
		for (String str : allProperties) {
			allName.put(str, str.substring(0, 1).toUpperCase() + str.substring(1));
		}
		return allName;
	}

	public String getClassName() {
		return className;
	}

	public static void main(String[] args) {
		new XmlParser();
	}
}
